// pages/waybilldeil/waybilldeil.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tid:0,
    num: 1,
  },
  // 减
  cannum: function () {
    var that = this;
    var param ={
      tid: that.data.tid,
      type:2
    };
    util.commonRequest('reduceVehicles', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        let num = that.data.num;
        num--;
        if (num <= 0) {
          num = 1;
        }
        that.setData({
          num: num
        })

        that.setData({
          orderDetail: data.data,
          // num: data.data.count
        })

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },

  //加
  addnum: function () {
    var that = this;
    let num = this.data.num;
    num++;
    this.setData({
      num: num
      // vehicles: num
    })
    var param = {
      tid: this.data.tid,
      type: 1
    };
    util.commonRequest('reduceVehicles', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        that.setData({
          orderDetail: data.data,
          // num: data.data.count
        })
       } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  initDetail:function( ){
    var that = this;
    var param = {
      tid: that.data.tid
    };
    wx.showLoading();
    util.commonRequest('myPublishDetail', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        that.setData({ 
          orderDetail: data.data,
          num: data.data.count
        })

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tid = options.tid;
    this.setData({
      tid:tid
    })
    this.initDetail()
  },
 lookStatus:function(e){
   var id = e.currentTarget.dataset.oid;
   app.navTo('../wbillstate/wbillstate?id=' + id)
 },
  querenzhuanghuoFun:function(id){
    var that = this;
    var param = {
      id: id
    }
    util.commonRequest('querenzhuanghuo', 'post', param, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.initDetail();
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  querenzhuanghuo:function(e){
    var id = e.currentTarget.dataset.oid;
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '请仔细查看订单信息,确认是否“装货”?',
      success(res) {
        if (res.confirm) {
          that.querenzhuanghuoFun(id);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  querenxiehuoFun:function(id){
    var that = this;
    var param = {
      id: id
    }
    util.commonRequest('querenxiehuo', 'post', param, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.initDetail();
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //货主确认卸货
  querenxiehuo:function(e){
    var id = e.currentTarget.dataset.oid;
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '请仔细查看订单信息,确认是否“卸货”?',
      success(res) {
        if (res.confirm) {
          that.querenxiehuoFun(id);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
 //同意接单
 accept:function(e){
   var oid = e.currentTarget.dataset.oid;
   var tid = e.currentTarget.dataset.tid;
   var that = this;
   var param = {
     tid: tid,
     oid:oid
   };

   wx.showLoading();
   util.commonRequest('acceptOrder', 'post', param, function (res) {
 
     wx.hideLoading();
      
     var data = res.data;
     if (data.code == 200) {
       wx.showToast({
         icon: 'loading',
         title: data.msg,
       })
       that.initDetail();
     } else {
       wx.showToast({
         icon: 'loading',
         title: data.msg,
       })
     }
   });
  //  acceptOrder
 },
 //拒绝接单
  Refuse:function(e){
    var oid = e.currentTarget.dataset.oid;
  
    var that = this;
    var param = {
      oid: oid
    };

    wx.showLoading();
    util.commonRequest('defineOrder', 'post', param, function (res) {

      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.initDetail();
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})